import {Component, OnInit} from '@angular/core';
import {Globals} from '../../../globals';
import {PageHelper} from '../../../utils/page-helper-model';
import {BaseService} from '../../../shared/service/base.service';
import {FormBuilder} from '@angular/forms';
import {MyDateModel} from '../../../models/my-date.model';
import {ApiUrl} from '../../../config/api-url';
import {Consts} from '../../../config/consts';
import {RoleIdEnum} from 'src/app/enums/role-id.enum';
import {TreeNodeInterface} from "../../../shared/interface/TreeNodeInterface";
import {ActivatedRoute} from "@angular/router";

@Component({
  selector: 'app-agency-statistic',
  templateUrl: './agency-statistic.component.html',
  styleUrls: ['./agency-statistic.component.less']
})
export class AgencyStatisticComponent implements OnInit {

  isMobile = Globals.isMobile();
  // 分页
  pageHelper: PageHelper = new PageHelper;

  listOfData = [];
  // 开始日期类型
  startDateTimeType: string = 'start';
  // 结束日期类型
  endDateTimeType: string = 'end';
  // 开始日期
  startDateTime: Date;
  // 结束日期
  endDateTime: Date;
  isCollapse = true;

  // 日月数据统计按钮样式
  day = 'primary';
  month = 'default';
  type = 1;

  mapOfExpandedData: { [key: string]: TreeNodeInterface[] } = {};

  constructor(private baseService: BaseService,
              private fb: FormBuilder ,private activatedRoute: ActivatedRoute) {
    this.activatedRoute.queryParams.subscribe(params => {
      this.startDateTime = this.baseService.zorro.getDateTime().startYesterday;
      this.endDateTime = this.baseService.zorro.getDateTime().endYesterday;
      if (params.username != null) {
        this.pageHelper.keyword = params.username;
        this.findData();
      } else {
        this.findData();
      }
    });
  }

  ngOnInit(): void {

  }

  // 将开始、结束时间往外抛
  outDateTime($event: MyDateModel) {
    /**
     * date: Fri Mar 19 2021 23:59:06 GMT+0800 (中国标准时间) {}
     * type: "end"
     */
    if ($event.type == 'start') {
      this.startDateTime = $event.date;
    } else if ($event.type == 'end') {
      this.endDateTime = $event.date;
    }
  }

  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findData();
  }

  findData() {
    const body = {
      createTime: this.startDateTime,
      updateTime: this.endDateTime,
      keyword: this.pageHelper.keyword.trim(),
      type: this.type
    };
    this.baseService.findListByMuchCondition(ApiUrl.StatsAgencyUrl + '/list', body, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.listOfData = data.data.data;
          if (this.listOfData.length > 0) {
            this.listOfData.forEach(item => {
              this.mapOfExpandedData[item.key] = this.baseService.zorro.convertTreeToList(item);
            });
          }
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  search() {
    this.pageHelper.currentPage = 1;
    this.findData();
  }

  onStartMonth($event) {
    this.startDateTime = $event;
  }
  onEndMonth($event: any) {
    this.endDateTime = $event;
  }


  dayMethod() {
    this.startDateTime = this.baseService.zorro.getDateTime().startYesterday;
    this.endDateTime = this.baseService.zorro.getDateTime().endYesterday;
    this.pageHelper.currentPage = 1;
    this.type = 1;
    this.findData();
    this.day = 'primary';
    this.month = 'default';
  }

  monthMethod() {
    this.startDateTime = this.baseService.zorro.getDateTime().startLastMonth;
    this.endDateTime = this.baseService.zorro.getDateTime().endLastMonth;
    this.pageHelper.currentPage = 1;
    this.type = 2;
    this.findData();
    this.day = 'default';
    this.month = 'primary';
  }
}
